/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：DistributeRegionContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseDistributeRegion;
import com.xteam.ecsystem.service.admin.DistributeRegionService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 配送区域控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class DistributeRegionContorller {

	@Autowired
	private DistributeRegionService bdrService;
	
	@Autowired
	private SystemLogService logService;	
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/lazyLoadRegionTreeGrid.do")
	public void lazyLoadRegionTreeGrid(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		//此处传递的id充当下一级的pid
		String bdrParent = ReqHelper.getRequestString(request, "bdrId");

		String bdrTree = this.bdrService.lazyLoadRegionTreeGrid(bdrParent);
		response.getWriter().write(bdrTree);

	}
	
	@RequestMapping("/delBdrById.do")
	public void delBdrById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bdrId = ReqHelper.getRequestString(request, "bdrId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.bdrService.delObject(bdrId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REGION_MGR, 
				"delObject", ConstantLocation.LOG_DELETE_RES, "BaseDistributeRegion");
	}
	
	@RequestMapping("/updateBdrStateById.do")
	public void updateBdrStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bdrId = ReqHelper.getRequestString(request, "bdrId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.bdrService.updateBdrStateById(bdrId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REGION_MGR, 
				"updateBdrStateById", ConstantLocation.LOG_Edit_STATE, "BaseDistributeRegion");
	}
	
	@RequestMapping("/getBaiListCob.do")
	public void getBaiListCob(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);

		String baiCob = this.bdrService.getBaiListCob();
		response.getWriter().write(baiCob);

	}
	
	@RequestMapping("/getBdrInfoById.do")
	public void getBdrInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bdrId = ReqHelper.getRequestString(request, "bdrId");		
		String strInfo = this.bdrService.getBdrInfoById(bdrId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/saveOrBdrInfo.do")
	public void saveOrBdrInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "regionInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseDistributeRegion bdr = (BaseDistributeRegion)JSONObject
				.toBean(obj, BaseDistributeRegion.class);
		
		if(StringUtil.isNotNullOrEmpty(bdr.getBdrId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REGION_MGR, 
				"saveOrUpdateObject", lslType, "BaseDistributeRegion");
		
		this.bdrService.saveOrUpdateObject(bdr, bdr.getBdrId());
	}
		
	@RequestMapping("/lazyLoadRegionTree.do")
	public void lazyLoadRegionTree(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		//此处传递的id充当下一级的pid
		String bdrParent = ReqHelper.getRequestString(request, "bdrId");

		String bdrTree = this.bdrService.lazyLoadRegionTree(bdrParent);
		response.getWriter().write(bdrTree);
	}
	
	@RequestMapping("/getBraByBdr.do")
	public void getBraByBdr(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bdrId = ReqHelper.getRequestString(request, "bdrId");
		String bdrType = ReqHelper.getRequestString(request, "bdrType");

		boolean isUsed = this.bdrService.getBraByBdr(bdrId, bdrType);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
}
